<?php
	require_once '../photo.php';
	require_once '../user.php'; 
    session_start();
	if (isset($_SESSION['username']))
	{
		$login = true;
		$loguname = $_SESSION["username"];
	}
	else{ $login = false; }
	
	$username = $_GET["un"];
	if ($username==NULL&&$login){$username=$loguname;}
	else if ($username==NULL&&!$login){
			
	}
	
	$path = 'users/'.$username.'/';
	$idfoto = $_GET["id"];
	$photor = photo::getPhoto($username,$idfoto);
	$photo = mysql_fetch_array($photor);
	if ($photo!=NULL){ 
		$valid = true;
		$_SESSION["lastpage"]='detail.php';
		$_SESSION["komenuname"] = $username;
		$_SESSION["komenidf"] = $idfoto;
	}else{
		$valid = false;
	}
	$bolehDiakses = false;
	if($login){
		$hubFollow = user::checkFollow($username,$loguname);
	}
	else{$hubFollow=0;}
	
	if ($username==$loguname||user::isRoleAdmin($loguname)){$bolehDiakses=true;}
	else if ($photo["HakAksesFoto"]==0){$bolehDiakses=true;}
	else if ($photo["HakAksesFoto"]==1){
		if ($hubFollow==1||$hubFollow==3){$bolehDiakses=true;}
	}
	else if ($photo["HakAksesFoto"]==2){
		if ($loguname==$username){$bolehDiakses=true;}
	}
	
	$bolehDikomen = false;
	if ($username==$loguname){
		$bolehDikomen=true;}
	else if ($photo["HakKomentarFoto"]==0){$bolehDikomen=true;	}
	else if ($photo["HakKomentarFoto"]==1){
		if ($hubFollow==1||$hubFollow==3){$bolehDikomen=true;}
	}else if ($photo["HakKomentarFoto"]==2){
		if ($loguname==$username){$bolehDikomen=true;}
	}

	$operation = $_GET['op'];
	if($operation == "big"){
		echo '<div id="contentphotodetail">';
		getBigPhoto($valid,$bolehDiakses,$path,$photo);
		echo '<div id="detarea" class="showdetail">';
		getDetail($valid,$bolehDiakses,$bolehDikomen,$path,$photo,$loguname);
		echo '</div></div>';
	}
	else if ($operation == "detail"){
		getDetail($valid,$bolehDiakses,$bolehDikomen,$path,$photo,$loguname);
	}
	else if($operation == "tag"){
		getDivEditTag($photo,$loguname); 
	}
	else if($operation == "edit"){
		getDivEditPhoto($photo,$loguname);
	}else{
		
	}
	
	function getBigPhoto($valid,$bolehDiakses,$path,$photo){
		echo '<div id="photoshowarea">';
			if ($valid&&$bolehDiakses){
				echo '<img id="bigphoto" alt="photo" src="'.$path.$photo["FileFoto"].'" />';
			}
			else if (!$valid){echo 'There is no photo to show';}
			else if (!$bolehDiakses){echo 'You have no authority to access this photo.';}
        echo '</div>';
	}
	
	function getDetail($valid,$bolehDiakses,$bolehDikomen,$path,$photo,$loguname){
		if ($valid&&$bolehDiakses){	
		$username = $photo["USERNAME"];
		$idfoto = $photo["IDFOTO"];
		?>
        
        <div id="phdetailleft" class="detailleft"><?php
            echo 'Photo uploaded by : '.'<br/>';
            echo '<p class="bigspace"><a class="unameh2" href="profile.php?'.$photo["USERNAME"].'">'.user::getName($photo["USERNAME"]).'</a></p><br/>';
            echo 'Uploaded on : '.$photo["tglUpload"].'<br/>';
            echo 'Taken on : '.$photo["tglAmbil"].'<br/><br/>';
        ?>
        <?php if ($username==$loguname){ ?>
            <div class="button180" onclick="photoDetailArea('edit','<?php echo $loguname; ?>','<?php echo $idfoto; ?>')">Edit Photo</div>
            <div class="button180" onclick="photoDetailArea('tag','<?php echo $loguname; ?>','<?php echo $idfoto; ?>')">Edit Tag</div>
            <div class="button180" onclick="setAvatar('<?php echo $loguname; ?>',<?php echo $idfoto; ?>)">Set As Avatar</div>
            <form name="avt" action="php/photoop.php" method="post">
                <input type="hidden" name="operation" value="avt"/>
            </form>
        <?php }
			if (user::isRoleAdmin($loguname)&&$loguname!=$username){?>
				<div class="button180" onclick="delPhoto('<?php echo $photo["USERNAME"]; ?>');">Delete As Admin</div>
                <form action="php/photoop.php" name="delphoto" method="post">
                	<input type="hidden" name="operation" value="delphoto" />
            	</form>
		<?php }?>
        	<br/>
		<?php if ($username!=$loguname){ ?>
            <div class="button180" onclick="divPhotoList('<?php echo $username; ?>');">Uploader's Gallery</div>
		<?php } ?>
            <div class="button180" onclick="divPhotoList('<?php echo $loguname; ?>');">My Gallery</div>
        </div>
        
        <div id="phdetailmiddle">
            <div id="toppanelcomment">
                <div id="nilai">
                    <div class="nilaibar">
                    <?php $totalNilai = photo::countLike($username, $idfoto, 0);//$photo["Likes"]+$photo["Dislikes"];?>
                        <div class="nilaibarlikes" style="width:<?php echo 100*photo::countLike($username, $idfoto, 1)/$totalNilai ?>%"></div>
                        <div class="nilaibardislikes" style="width:<?php echo 100*photo::countLike($username, $idfoto, -1)/$totalNilai ?>%"></div>
                    </div>
                <?php
                     echo '<div class="nilaiteks">'.photo::countLike($username, $idfoto, 1).' Likes, '.photo::countLike($username, $idfoto, -1).' Dislikes</div>';
                ?>
                </div>
                <?php if (photo::canLike($username,$idfoto,$loguname)){ ?>
                <div class="nilaicontrol">
                    <div id="likebutt" class="likedislikebutt" onclick="doLike('<?php echo $username; ?>','<?php echo $idfoto; ?>');"></div>
                    <form name="like" action="php/photoop.php" method="post">
                        <input type="hidden" name="operation" value="like"/>
                    </form>
                    <div id="dislikebutt" class="likedislikebutt" onclick="doDislike('<?php echo $username; ?>','<?php echo $idfoto; ?>')"></div>
                    <form name="dislike" action="php/photoop.php" method="post">
                        <input type="hidden" name="operation" value="dislike"/>
                    </form>
                </div>
                <?php } ?>
                <?php if ($bolehDikomen){ ?>
                <br/><a class="commentbutton" href="javascript:void()" onclick="show('commentinput');$('cominput').focus();">Add Comment</a>
                <?php } ?>
            </div>
            <div id="botpanelcomments">
			<?php	require 'photo_comments.php'; ?>
            </div>            
            <div id="commentinput" class="boxcomment" >
                <div class="minava"><?php echo'<img class="minava" src="'.photo::getProfPicPath($loguname).'" />'?></div>
                <div class="commpl">
                    <div style="text-align:left;padding-left:5px;font-size:12px;color:#333">
                       Fill Your Comment
                    </div>
                    <div class="comment">
                    <form action="php/photoop.php" method="post" name="addcomment" onsubmit="addComment('<?php echo $username; ?>',<?php echo $idfoto; ?>);return false;";>
                        <input style="width:330px;" type="text" name="cominput" id="cominput" class="cominput"  onblur="hide('commentinput');$(this.id).value='';"/>
                        <input type="hidden" name="operation" value="addcomment" />
                    </form>
                    </div>
                </div>
            </div>
        </div>
        
        <div id="phdetailright">
            <div class="teksh5">Description</div><?php echo '<p>'.$photo["Deskripsi"].'</p>'; ?>
            <div class="teksh5">Kategori</div><?php 
                require_once '../database.php';
                $katttext = database::query("SELECT * FROM Kategori WHERE IDKategori=' $photo[IDKategori]'");
                $kattext = mysql_fetch_array($katttext); 
                echo'<p>'.$kattext["Keterangan"].'</p>'; 
            ?>
            <div class="teksh5">Keyword</div><?php echo '<p>Letak Keyword-keyword</p>'; ?>
            <div class="teksh5">Tagged On Photo</div><?php 
                $tag = photo::getTagged($username, $idfoto);
                $count = 0;
                while ($row = mysql_fetch_array($tag)){
                    $count += 1;
                    echo '<a class="unameh2" href="profile?'.$row["Usernametagged"].'">'.user::getName($row["Usernametagged"]).'</a><br/>';
                }
                if ($count==0) echo '<p>None</p>';
                ?>
        </div>
    <?php	}	
	}
	
	function getDivEditPhoto($photo,$loguname){ 
		$idfoto = $photo["IDFOTO"];		?>
        <div id="editshowleft" class="detailleft">
            <div class="button180" onclick="editPhoto('<?php echo $loguname; ?>','<?php echo $idfoto; ?>');">Save Change</div>
            <div class="button180" onclick="photoDetailArea('detail','<?php echo $loguname; ?>','<?php echo $idfoto; ?>');">Cancel</div>
            <br/><br/><br/><br/><br/>
            <div class="button180" onclick="delPhoto('<?php echo $photo["USERNAME"]; ?>');">Delete Photo</div>
            <form action="php/photoop.php" name="delphoto" method="post">
                <input type="hidden" name="operation" value="delphoto" />
            </form>
        </div>
        <div id="editshowleft" class="detailbigright">
        <form action="php/photoop.php" name="editphoto" method="post">
            <input type="hidden" name="operation" value="editdetail" />
            <div class="editbox">Description :
                <textarea class="dtextbox" type="text" name="idesc" rows="5"><?php echo $photo["Deskripsi"]; ?></textarea>
            </div><div class="editbox"> Keyword :
                <textarea class="dtextbox" type="text" name="ikeyword" rows="2"><?php echo $photo["Keyword"]; ?></textarea>
            </div><div class="editbox">Category :
                <select class="dtextbox" type="text" name="kategori" />
                <?php
                    $cate = photo::getCategory();
                    $index = 1;
                    while ($crow = mysql_fetch_array($cate)){
                        echo '<option value="'.$index.'" ';
                        if ($photo["IDKategori"]==$index) echo ' selected="selected"';
                        echo '>'.$crow["Keterangan"].'</option>';	
                        $index++;
                    }
                ?>
                </select>
            </div><div class="editbox">Comment :
                <select class="dtextbox" name="hakkomen">
                  <option value="0" <?php if($photo["HakKomentarFoto"]==0) echo 'selected="selected"';?>>Everyone</option>
                  <option value="1" <?php if($photo["HakKomentarFoto"]==1) echo 'selected="selected"';?>>Follower Only</option>
                  <option value="2" <?php if($photo["HakKomentarFoto"]==2) echo 'selected="selected"';?>>Me Only</option>
                </select>
            </div><div class="editbox">Access :
                <select class="dtextbox" name="hakakses">
                  <option value="0" <?php if($photo["HakAksesFoto"]==0) echo 'selected="selected"';?>>Everyone</option>
                  <option value="1" <?php if($photo["HakAksesFoto"]==1) echo 'selected="selected"';?>>Follower Only</option>
                  <option value="2" <?php if($photo["HakAksesFoto"]==2) echo 'selected="selected"';?>>Me Only</option>
                </select>
            </div>
        </form>
        </div>
<?php
	}
	
	
	function getDivEditTag($photo,$loguname){ 
		$idfoto = $photo["IDFOTO"];		
	?>
        <div id="tagshowleft" class="detailleft" style="border-right:solid 1px #999;">
            <div class="button180" onclick="doTag('<?php echo  $photo["USERNAME"]; ?>','<?php echo $idfoto; ?>');">Do Tag</div>
            <div class="button180" onclick="photoDetailArea('detail','<?php echo $loguname; ?>','<?php echo $idfoto; ?>')">Cancel</div>
            <br/><br/>
            <div class="button180" onclick="document.forms[''].submit();">Remove All Tag</div>
        </div>    
        <div id="tagshowleft" class="detailbigright" style="border:none">
            <form action="php/photoop.php" name="tagphoto" method="post">
                <input type="hidden" name="operation" value="tagphoto" />
                <div class="editbox">Tagged User :
                <textarea id='taglist' class="dtextbox" type="text" name="taglist" rows="5"><?php
                $tag = photo::getTagged($loguname, $idfoto);
                while ($row = mysql_fetch_array($tag)){
                    echo '@'.$row["Usernametagged"].', ';
                }  ?></textarea>
                </div>
            </form>
        </div>
<?Php
	}
?>